<template>
  <div class="article-container">
    <!-- 导航栏 -->
    <van-nav-bar
      fixed
      left-arrow
      @click-left="$router.back()"
      title="文章详情"
    ></van-nav-bar>
    <!-- /导航栏 -->

    <!-- 加载中 loading -->
    <van-loading class="article-loading" v-if="isLoading" />
    <!-- /加载中 loading -->
    <div v-if="is404">文章不见了</div>
    <!-- 文章详情 -->
    <div class="detail" v-else>
      <h3 class="title">{{ article.title }}</h3>
      <div class="author">
        <van-image
          round
          width="1rem"
          height="1rem"
          fit="fill"
          :src="article.aut_photo"
        />
        <div class="text">
          <p class="name">{{ article.aut_name }}</p>
          <p class="time">{{ article.pubdate | rTime }}</p>
        </div>
        <van-button round size="small" type="info" @click="Hfollowed">
          {{ article.is_followed ? "已关注" : "+ 关注" }}
        </van-button>
      </div>
      <div class="content">
        <!-- 由于正文是html文档 所以这里要用v-html -->
        <div v-html="article.content"></div>
      </div>
      <van-divider>END</van-divider>
      <div class="zan">
        <van-button
          round
          size="small"
          hairline
          type="primary"
          plain
          :icon="article.attitude === 1 ? 'good-job' : 'good-job-o'"
          @click="hSwitchLike"
          >{{ article.attitude === 1 ? "取消点赞" : "点赞" }}</van-button
        >&nbsp;&nbsp;&nbsp;&nbsp;
        <van-button
          round
          size="small"
          hairline
          type="danger"
          plain
          :icon="article.attitude === 0 ? 'like' : 'delete'"
          @click="hSwitchDisLike"
          >{{ article.attitude === 0 ? "取消不喜欢" : "不喜欢" }}</van-button
        >
      </div>
    </div>
    <!-- /文章详情 -->
    <!-- 评论模块 -->
    <!-- 这里 article初始化的时候是一个空对象 所以要价格判断 否则会报错-->
    <Comment :artid="articleId.toString()" />
  </div>
</template>

<script>
import {
  detail,
  addLike,
  delLike,
  addDisLike,
  deleteDisLike
} from "@/api/article.js";
import { followUser, unFollowUser } from "@/api/user.js";
import Comment from "./comment";
export default {
  name: "ArticleIndex",
  data() {
    return {
      loading: true, // 控制加载中的 loading 状态
      article: {},
      articleId: null,
      isLoading: true,
      is404: false
    };
  },
  components: {
    Comment
  },
  created() {
    // console.log(this.$route, this.$router);//小技巧，不知道在哪，打印出来看看
    this.articleId = this.$route.params.id; //参数在路由的获取方式
    this.getDetail();
  },
  //触发时机  离开页面的时候  也就是 to的时候
  //1.来源 from 肯定是文章详情
  //2.to  首页是正常的
  //3 to  登录页不希望
  beforeRouteLeave(to, from, next) {
    // console.log(to, "to方法");
    // console.log(from, "from方法");
    // console.log(next, "next方法");
    //1 去哪里  登录页
    //获取token
    const token = this.$store.state.tokenInfo.token;
    if (to.path === "/login") {
      if (!token) {
        next(); //否则去登录页  next直接放行
      } else {
        next("/"); //登录了去 首页
      }
    } else {
      next(); //去下一个页面
    }
  },
  methods: {
    async getDetail() {
      try {
        const res = await detail(this.articleId);
        console.log(res);
        this.article = res.data.data;
        this.isLoading = false; //取消加载状态
      } catch (err) {
        console.log("获取失败");
        this.isLoading = false; //取消加载状态
        if (err && err.response && err.response.status === 404) {
          // alert('文章不见了')
          this.is404 = true;
        }
      }
    },
    //关注/取消关注
    async Hfollowed() {
      //获取用户的id
      const userId = this.article.aut_id.toString();
      //判断我们这个是不是被关注的
      if (this.article.is_followed) {
        try {
          //关注了我们调用取消关注的接口
          await unFollowUser(userId);
          this.$toast("取消关注成功");
          //成功
          this.article.is_followed = false;
        } catch {
          //失败
          this.$toast("取消关注失败");
        }
      } else {
        try {
          //没关注就调用关注接口
          await followUser(userId);
          //成功
          this.$toast("关注成功");
          this.article.is_followed = true;
        } catch {
          //失败
          this.$toast("关注失败");
        }
      }
    },
    //点赞/取消点赞
    async hSwitchLike() {
      //获取文章id
      const article_id = this.articleId;
      //判断这篇文章是否已经被点赞
      if (this.article.attitude === 1) {
        //已经被点赞，那就取消点赞
        await delLike(article_id);
        this.article.attitude = -1;
      } else {
        await addLike(article_id);
        this.$toast.success("操作成功");
        this.article.attitude = 1;
      }
    },
    //作者态度 -1无态度  0  不喜欢  1 点赞
    //切换是否不喜欢
    async hSwitchDisLike() {
      //   获取文章id
      const article_id = this.articleId;
      if (this.article.attitude === 0) {
        await deleteDisLike(article_id);
        this.article.attitude = -1;
      } else {
        try {
          await addDisLike(article_id);
          this.article.attitude = 0;
        } catch (err) {
          console.log(err);
        }
      }
      this.$toast.success("操作成功");
    }
  }
};
</script>

<style scoped lang="less">
.article-container {
  position: absolute;
  left: 0;
  top: 0;
  overflow-y: scroll;
  width: 100%;
  height: 100%;
}
.article-loading {
  padding-top: 100px;
  text-align: center;
}
.error {
  padding-top: 100px;
  text-align: center;
}
.detail {
  padding: 50px 10px;
  .title {
    font-size: 16px;
  }
  .zan {
    text-align: center;
  }
  .author {
    padding: 10px 0;
    display: flex;
    .text {
      flex: 1;
      padding-left: 10px;
      line-height: 1.3;
      .name {
        font-size: 14px;
        margin: 0;
      }
      .time {
        margin: 0;
        font-size: 12px;
        color: #999;
      }
    }
  }
  .content {
    font-size: 14px;
    overflow: hidden;
    white-space: pre-wrap;
    word-break: break-all;
    /deep/ img {
      max-width: 100%;
      background: #f9f9f9;
    }
  }
}
</style>
